"""
URL configuration for OMS project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from apps.UMM.views.attendance_views import AttendanceViewSet, test_attendance_api
from apps.UMM.views.user_views import UserViewSet, test_api_endpoint
from apps.UMM.views.salary_views import (
    SalaryListAPI, SalaryDetailAPI, SalaryExportAPI, 
    CustomerServiceUserListAPI, test_salary_api,
    generate_test_salary_data, generate_test_salary_detail
)

# 创建用户视图集路由器 - 放在最前面优先匹配
user_router = DefaultRouter()
user_router.register(r'users', UserViewSet, basename='user')

# 创建考勤专用路由器
attendance_router = DefaultRouter()
attendance_router.register(r'attendance', AttendanceViewSet)

# 为前端尝试的所有API路径创建额外的路由器
frontend_user_router = DefaultRouter()
frontend_user_router.register(r'users', UserViewSet, basename='frontend-user')
frontend_user_router.register(r'user', UserViewSet, basename='frontend-user-singular')

# 服务人员路由器
service_staff_router = DefaultRouter()
service_staff_router.register(r'users/service-staff', UserViewSet, basename='service-staff')

urlpatterns = [
    # 直接添加高优先级测试API路径 - 无需认证，用于测试
    path("api/test-users/", test_api_endpoint, name='test_api_users'),
    path("api/umm/test-api/", test_api_endpoint, name='test_api_endpoint'),
    path("api/umm/test-salary/", test_salary_api, name='test_salary_api'),
    path("api/umm/test-attendance/", test_attendance_api, name='test_attendance_api'),
    
    # 添加测试数据生成API
    path("api/umm/test-salary-data/", generate_test_salary_data, name='test_salary_data'),
    path("api/umm/test-salary-detail/", generate_test_salary_detail, name='test_salary_detail'),
    
    # 最高优先级 - 用户API路径（确保这些路径在include之前定义）
    path("api/umm/users/", UserViewSet.as_view({'get': 'list', 'post': 'create'}), name='user-list-api'),
    path("api/umm/users/<int:pk>/", UserViewSet.as_view({'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'}), name='user-detail-api'),
    path("api/umm/users/batch-delete/", UserViewSet.as_view({'post': 'batch_delete'}), name='user-batch-delete-api'),
    path("api/umm/users/export/", UserViewSet.as_view({'get': 'export_users'}), name='user-export-api'),
    
    # 为前端添加额外的API路径
    path("api/users/", UserViewSet.as_view({'get': 'list', 'post': 'create'}), name='frontend-user-list-api'),
    path("api/users/<int:pk>/", UserViewSet.as_view({'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'}), name='frontend-user-detail-api'),
    path("api/user/", UserViewSet.as_view({'get': 'list', 'post': 'create'}), name='frontend-user-singular-list-api'),
    path("api/user/<int:pk>/", UserViewSet.as_view({'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'}), name='frontend-user-singular-detail-api'),
    path("api/umm/user/", UserViewSet.as_view({'get': 'list', 'post': 'create'}), name='frontend-umm-user-singular-list-api'),
    path("api/umm/user/<int:pk>/", UserViewSet.as_view({'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'}), name='frontend-umm-user-singular-detail-api'),
    
    # 服务人员API路径
    path("api/umm/users/service-staff/", UserViewSet.as_view({'get': 'service_staff'}), name='service-staff-list'),
    
    # 添加个人资料API路径
    path("api/umm/users/profile/", UserViewSet.as_view({'get': 'get_profile', 'put': 'update_profile'}), name='user-profile'),
    path("api/umm/users/password/", UserViewSet.as_view({'put': 'change_password'}), name='user-change-password'),
    
    # 考勤记录直接路径 - 优先于include路径
    path("api/umm/attendance/", AttendanceViewSet.as_view({'get': 'list', 'post': 'create'}), name='attendance-list'),
    path("api/umm/attendance/<int:pk>/", AttendanceViewSet.as_view({'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'}), name='attendance-detail'),
    path("api/umm/attendance/batch_delete/", AttendanceViewSet.as_view({'post': 'batch_delete'}), name='attendance-batch-delete'),
    path("api/umm/attendance/export/", AttendanceViewSet.as_view({'get': 'export'}), name='attendance-export'),
    
    # 薪资相关API路径 - 确保所有前端需要的路径都直接定义（添加末尾斜杠以匹配前端请求）
    path("api/umm/salary/list", SalaryListAPI.as_view(), name='salary-list-api'),
    path("api/umm/salary/list/", SalaryListAPI.as_view(), name='salary-list-api-slash'),
    path("api/umm/salary/detail", SalaryDetailAPI.as_view(), name='salary-detail-api'),
    path("api/umm/salary/detail/", SalaryDetailAPI.as_view(), name='salary-detail-api-slash'),
    path("api/umm/salary/export", SalaryExportAPI.as_view(), name='salary-export-api'),
    path("api/umm/salary/export/", SalaryExportAPI.as_view(), name='salary-export-api-slash'),
    path("api/umm/salary/user-options", CustomerServiceUserListAPI.as_view(), name='salary-user-options-api'),
    path("api/umm/salary/user-options/", CustomerServiceUserListAPI.as_view(), name='salary-user-options-api-slash'),
    
    # 为了前端开发测试，增加无斜杠和简化版本的API路径
    path("api/salary/list", SalaryListAPI.as_view(), name='simple-salary-list-api'),
    path("api/salary/list/", SalaryListAPI.as_view(), name='simple-salary-list-api-slash'),
    path("api/salary/detail", SalaryDetailAPI.as_view(), name='simple-salary-detail-api'),
    path("api/salary/detail/", SalaryDetailAPI.as_view(), name='simple-salary-detail-api-slash'),
    path("api/salary/export", SalaryExportAPI.as_view(), name='simple-salary-export-api'),
    path("api/salary/export/", SalaryExportAPI.as_view(), name='simple-salary-export-api-slash'),
    path("api/salary/user-options", CustomerServiceUserListAPI.as_view(), name='simple-salary-user-options-api'),
    path("api/salary/user-options/", CustomerServiceUserListAPI.as_view(), name='simple-salary-user-options-api-slash'),
    
    # 管理界面
    path("admin/", admin.site.urls),
    
    # 应用包含的URL配置 - 放在最后，让上面的直接路径有更高的优先级
    path("api/omm/", include("apps.OMM.urls")),
    path("api/umm/", include("apps.UMM.urls")),
    
    # 使用路由器 - 放在最后，让上面的直接路径有更高的优先级
    path("api/umm/", include(user_router.urls)),  
    path("api/", include(frontend_user_router.urls)),
    path("api/umm/", include(service_staff_router.urls)),
    path("api/", include(attendance_router.urls)),
]
